<?php
//第二版

namespace App\Http\Controllers;

/**
 * 防止不同事务处理，做了区分
 * Class HandleCurl2
 * @package App\Http\Controllers
 */
class HandleCurl2 extends \Curl\Curl
{
    protected static $both = [];//分成两个主体

    protected static $other = [];

    protected static $table = [];

    protected static $result = [];

    protected static $tableId;

    protected static $result_status;

    protected static $result_time;

    /**
     * 处理数据返回值(分成两个主体)
     * @param array $other
     * @param array $table
     * @param array $result
     */
    public function both($other = [], $table = [], $result = [])
    {
        self::$both = compact('other', 'table', 'result');
        self::$other = $other;
        self::$table = $table;
        self::$result = $result;
    }

    /**
     * 处理数据
     * @param string $content
     * @return HandleCurl2|bool
     */
    public static function handleData($content = '')
    {
        $_this = (new self());
        $both = explode(',', $content);
        if (count($both) >= 6) {
            $_this->both(array_splice($both, 0, 3), array_splice($both, 0, 3), $both);
            return $_this;
        }
        return false;
    }

    /**
     * 其他参数
     * @return array
     */
    public function other()
    {
        //5-69880/播放地址帧,（20201021/时间 _ 102/台数 _ 02/局数 - 34/场数） ,/未知（时有时无）,
        $default = ['play_address', 'data', 'undefined'];
        foreach (self::$other as $k => $v) {
            self::$other[$default[$k]] = $v;
            unset(self::$other[$k]);
        }
        if (isset(self::$other['data'])) self::$other['data'] = self::otherData(self::$other['data']);
        return self::$other;
    }

    private function otherData($otherData = '')
    {
        $otherData = exploders(['_', '-'], $otherData);
        $default = ['time', 'table_id', 'office_id', 'site_id'];
        foreach ($otherData as $k => $v) {
            $otherData[$default[$k]] = $v;
            unset($otherData[$k]);
        }
        return $otherData;
    }

    public function table()
    {
        //102/台数,30/时间,1/开奖状态
        $default = ['table_id', 'result_time', 'result_status'];//台数  开奖时间  开奖状态
        foreach (self::$table as $k => $v) {
            self::$table[$default[$k]] = $v;
            unset(self::$table[$k]);
        }
        return self::$table;
    }

    /**
     * 桌子id
     * @return mixed
     */
    public function tableId()
    {
        foreach (self::$table as $k => $v) if ($k == 'table_id') {
            return self::$tableId = $v;
        }
    }

    /**
     * 开奖状态
     * @return mixed
     */
    public function result_status()
    {
        foreach (self::$table as $k => $v) if ($k == 'result_status') {
            return self::$result_status = $v;
        }
    }

    /**
     * 开奖时间
     * @return mixed
     */
    public function result_time()
    {
        foreach (self::$table as $k => $v) if ($k == 'result_time') {
            return self::$result_time = $v;
        }
    }

    /**
     * 开奖号码
     * @return array
     */
    public function result()
    {
        foreach (self::$result as $k => $v) {
            self::$result['num' . ($k + 1)] = $v;
            unset(self::$result[$k]);
        }
        return self::$result;
    }

    public function all()
    {
        $other = self::other();
        $table = self::table();
        $result = self::result();
        return compact('other', 'table', 'result');
    }
}
